<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
		<meta name="mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="theme-color" content="#2196f3">
		<title>天择可付</title>
		<link rel="stylesheet" href="../css/mui.min.css">
		<link rel="stylesheet" href="../css/home.css">
		<script type="text/javascript" src="../js/config.js"></script>
		<script type="text/javascript" src="../js/jquery-2.1.0.js"></script>
		<script type="text/javascript" src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/mobile-detect.min.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript" src="../controll/index.js"></script>
		<script type="text/javascript">
			// 		mui.init();
			// 		muiBack();

			document.addEventListener("plusready", function() {
				var ws = null;
				var socketTime = null;
				var socketSign = false;
				createdSocket();
				// 创建即时通讯 
				function createdSocket() {
					ws = new WebSocket("wss://online02.tiaze.com:8282");
					// 服务端主动推送消息时会触发这里的onmessage
					if (ws) {
						ws.onclose = function(evnt) {
							// console.log('websocket服务关闭了');
							if (socketTime) {
								return false;
							}
							ws = null;
							socketTime = setInterval(function() {
								createdSocket();
							}, 3000);
						};
						ws.onerror = function(evnt) {
							// console.log('websocket服务出错了');
							if (socketTime) {
								return false;
							}
							ws = null;
							socketTime = setInterval(function() {
								createdSocket();
							}, 3000);
						};
						ws.onmessage = function(e) {
							// console.log('接收到了消息');
							if (socketTime) {
								clearInterval(socketTime);
							}
							socketTime = null;
							socketSign = false;
							// json数据转换成js对象
							var data = eval("(" + e.data + ")");
							var type = data.type || '';
							console.log(type);
							switch (type) {
								// Events.php中返回的init类型的消息，将client_id发给后台进行uid绑定
								case 'ping':
									break;
								
                             
								case 'login':
									var ss = {
										type: 'login',
										login_type: '2',
										client_id: data.client_id,
										user_phone: localStorage.getItem("USERID")
									};
									xhrCreate('https://online02.tiaze.com/WorkPost', ss);
									console.log(JSON.stringify(data));
									//$.post('https://pay.tiaze.com/WorkPost', , 'json');
									// 当mvc框架调用GatewayClient发消息时直接alert出来
								default:
								    console.log('default');
									var data = JSON.parse(e.data);
									console.log(data.msg.type == 'SMILE');
									if(data.msg.type == 'SMILE'){
										
										console.log('SMILE');
										console.log(JSON.stringify(data));
										payAudio("../audio/4182.mp3");
										var conf =
											data.msg.title + "\n" + data.msg.orderid + " " + data.msg.amount + "元"+"<p class='returnTitle' style='text-align:left;'>备注：</p>";
										conf +=
											"<div class='code-phone'><input class='remark_shack'  type='text' value=''></div>";
										mui.confirm(conf, " ", ["确认", "取消"], function(e) {
											var remark = $(".remark_shack").val();
											console.log(remark);
											if (e.index === 0) { //点击打印
												if(remark != ""){
													if (data.msg.print_body) {
														data.msg.print_body[7]= '备注：'+ remark + '\\n';
														// console.log(data.msg.print_body);
														printTicket(data.msg.print_body);
														
														
													}
													
													require(weburl + smile, "json", "post", {
														token: localStorage.getItem("token"),
														out_trade_no:data.msg.orderid,
														remark:remark
													}, function(response) {
														console.log(JSON.stringify(response));
														
														openwindow("detail.html", "detail.html", {
															orderNum: data.msg.orderid
														});
													}, function() {
														
														mui.toast('网络错误', {
															duration: 'long',
															type: 'div'
														})
													})
												} else {
												  data.msg.print_body[7]= '备注：无备注\\n';
												  // console.log(data.msg.print_body);
												  printTicket(data.msg.print_body);
												  openwindow("detail.html", "detail.html", {
												  	orderNum: data.msg.orderid
												  });
												}
												
											} else {
												
												openwindow("detail.html", "detail.html", {
													orderNum: data.msg.orderid
												});
											}
										}, "div");
										return false;
									} else if (data.msg.type == 'SMILEAUTH'){
										
										console.log('SMILEAUTH');
										console.log(JSON.stringify(data));
										payAudio("../audio/4182.mp3");
										var conf =
											data.msg.title + "\n" + data.msg.orderid + " " + data.msg.amount + "元"+"<p class='returnTitle' style='text-align:left;'>备注:</p>";
										conf +=
											"<div class='code-phone'><input class='remark_shack'  type='text' value=''></div>";
										mui.confirm(conf, " ", ["确认", "取消"], function(e) {
											var remark = $(".remark_shack").val();
											console.log(remark);
											if (e.index === 0) { //点击打印
											    if(remark != ""){
													// 备注不为空时打印修改
											    	if (data.msg.print_body) {
											    		data.msg.print_body[7]= '\\n备注：'+ remark + '\\n'
														// console.log(data.msg.print_body);
											    		printTicket(data.msg.print_body);
											    		
											    		
											    	}
													require(weburl + smileFundAuth, "json", "post", {
														token: localStorage.getItem("token"),
														out_order_no:data.msg.orderid,
														remarks:remark
													}, function(response) {
														console.log(JSON.stringify(response));
														
														openwindow("depositDetail.html", "depositDetail.html", {
															orderNum: data.msg.orderid
														});
													}, function() {
														
														mui.toast('网络错误', {
															duration: 'long',
															type: 'div'
														})
													})
												}else {
													data.msg.print_body[7]= '\\n备注: 无备注\\n';
													// console.log(data.msg.print_body);
													printTicket(data.msg.print_body);
													openwindow("depositDetail.html", "depositDetail.html", {
														orderNum: data.msg.orderid
													});
												}
												
											} else {
												openwindow("depositDetail.html", "depositDetail.html", {
													orderNum: data.msg.orderid
												});
											}
										}, "div")
										return false;
									} else if (data.msg) {
										
										//语音提醒
										console.log(JSON.stringify(data));
										payAudio("../audio/4182.mp3");
										//var msg = JSON.parse(data.msg);
										plus.nativeUI.confirm(data.msg.title + "\n" + data.msg.orderid + " " + data.msg.amount + "元", function(ev) {
											if (ev.index === 1) { //点击打印
												if (data.msg.print_body) {
													var str = data.msg.print_body;
													var reg = /\\n/g;
													str = str.replace(reg, '\n');
													//												printTicket(data.msg.print_array);
													ticket(data.msg.store_name, str, data.msg.orderid);
													return false;
												}
												alert("系统繁忙，请稍后再试");
												return false;
											} else {
												orderType(data.msg.type, data.msg.orderid)
											}
										}, "您有新订单提醒", ["详情(关闭)", "打印"]);
									}
							}
						};
					}
					var xhr = null;

					function xhrCreate(url, data) {
						if (xhr) {
							return;
						}
						xhr = new plus.net.XMLHttpRequest();
						xhr.onreadystatechange = function() {
							switch (xhr.readyState) {
								case 0:
									break;
								case 1:
									break;
								case 2:
									break;
								case 3:
									break;
								case 4:
									if (xhr.status == 200) {
										//alert('请求成功');
									} else {}
									break;
								default:
									break;
							}
						}
						xhr.open("POST", url);
						xhr.setRequestHeader('Content-Type', 'application/json');
						xhr.send(data);
					}
				}

			}, false);
		</script>
	</head>

	<body>
		<div id="div1">
			<div id="div1_tou">
				<span id="app-title">天择可付</span>
			</div>
			<style type="text/css">
				.modal-item {
					width: 32%;
					display: inline-block;
					text-align: center;
					color: #fff;
					padding-top: 20px;
				}

				.modal-item img {
					width: 40px;
					margin-bottom: 5px;
				}

				#app-title {
					width: 200px;
					display: inline-block;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}
			</style>
			<div class="mainModal">
				<div class="modal-item" data-url="pay.html">
					<img src="../img/ic_receivables@2x.png" />
					<div>普通收款</div>
				</div>
				<div class="modal-item" data-url="deposit.html">
					<img src="../img/ic_home_empower_receivables@2x.png" />
					<div>押金收款</div>
				</div>
				<div class="mainLeft modal-item" data-url="onlineBooking.html">
					<img src="../img/ic_home_online_receivables@2x.png" />
					<div>在线预授权</div>
				</div>
			</div>
		</div>

		<!--投资保障-->
		<div>
			<div style="width: 100%;">
				<ul class="mui-table-view mui-grid-view mui-grid-9" style="padding-bottom: 15px;">
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="loan.html" data-id="loan">
						<a href="JavaScript:;">
							<img src="../img/ic_periodization@2x.png" />
							<div class="mui-media-body">分期收款</div>
						</a>
					</li>
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="my/zhangben/timesearch.html" data-id="timesearch">
						<a href="JavaScript:;">
							<img src="../img/ic_statistics@2x.png" style="padding: 2px;" />
							<div class="mui-media-body">押金统计</div>
						</a>
					</li>
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="printCount.html" data-id="printCount.html">
						<a href="JavaScript:;">
							<img src="../img/ic_home_printing@2x.png" style="padding: 2px;" />
							<div class="mui-media-body">打印汇总</div>
						</a>
					</li>
					<!--<li class="mui-table-view-cell mui-media mui-col-xs-4 mui-col-sm-3" data-url="zhang.html" data-id="zhang" data-sign="home">
						<a href="JavaScript:;">
							<img src="../img/zhang.png" style="padding: 2px;" />
							<div class="mui-media-body">账单管理</div>
						</a>
					</li>-->
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="public/scanning.html" data-id="scanning.html">
						<a href="JavaScript:;">
							<img src="../img/ic_home_scan@2x.png" />
							<div class="mui-media-body">扫一扫</div>
						</a>
					</li>
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="sp.html" data-id="sp">
						<a href="JavaScript:;">
							<img src="../img/ic_home_com_management@2x.png" />
							<div class="mui-media-body">商品管理</div>
						</a>
					</li>
					<!--<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="reservation.html" data-id="reservation">
						<a href="JavaScript:;">
							<img src="../img/hotel.png" />
							<div class="mui-media-body">预订管理</div>
						</a>
					</li>-->
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="spsk.html" data-id="spsk">
						<a href="JavaScript:;">
							<img src="../img/ic_gathering@2x.png" />
							<div class="mui-media-body">商品收款</div>
						</a>
					</li>
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="qygl.html" data-id="qygl">
						<a href="JavaScript:;">
							<img src="../img/ic_mine_manage copy@2x.png" />
							<div class="mui-media-body">签约管理</div>
						</a>
					</li>
					<li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3" data-url="shouyinyuan.html" data-id="shouyinyuan">
						<a href="JavaScript:;">
							<img src="../img/ic_home_cashier@2x.png" />
							<div class="mui-media-body">收银员</div>
						</a>
					</li>
				</ul>
			</div>
		</div>
		<!--消息通知-->
		<div class="news" id="news">

		</div>
		<!--活动-->
		<a href="JavaScript:;" class="active_pt">

		</a>

		<!--昨日累计-->
		<div class="" style="background-color: white; height: 40px;line-height: 40px; margin-top: 10px; border-bottom: 1px solid #E7E7E7;padding-left: 15px;">昨日累计</div>
		<div class="countAndPeo">
			<div class="left">
				<p class="danwei">
					<span class="lanzi price">--</span>元</p>
				<p class="new">收款金额</p>
			</div>
			<div class="left">
				<p class="danwei">
					<span class="lanzi countAll">--</span>笔</p>
				<p class="new">成交笔数</p>
			</div>
		</div>

		<script>
			mui.init({
				pullRefresh: {
					container: '#news',
					down: {
						style: 'circle',
						callback: pulldownRefresh
					}
				}
			});
			muiBack();
			/**
			 * 消息模板刷新
			 * 下拉刷新具体业务实现
			 */

			//			notice_websokit();

			function pulldownRefresh() {
				news();
				mui('#news').pullRefresh().endPulldown();
			}

			news();

			//最新消息提示
			function news() {
				require(weburl + getFirNew, "json", "post", {
					token: localStorage.getItem("token")
				}, function(response) {
					console.log(JSON.stringify(response));
					if (response.status == "success") {
						var html = '<div class="news-v"><img src="../img/notice.png">';
						html += '<p>' + response.data[0].title + '';
						html += '<span class="mui-icon mui-icon-forward"></span></p></div>';
						$("#news").html(html);
						return false;
					} else if (response.code == "10002") {
						// 移除消息
						var html = '';
						$("#news").html(html);
					}

				}, function(xhr) {
					console.log(JSON.stringify(xhr));
					mui.toast('网络错误', {
						duration: 'long',
						type: 'div'
					})
				})
			}

			//昨日累计
			require(weburl + model1, "json", "post", {
				token: localStorage.getItem("token"),
				time_start: getYesterday() + " " + "00:00:00",
				time_end: getYesterday() + " " + "23:59:59"
			}, function(response) {
				$(".price").html(response.count.amount);
				$(".countAll").html(response.count.bisu);
			}, function() {
				mui.toast('网络错误', {
					duration: 'long',
					type: 'div'
				})
			})

			// app信息
			require(weburl + appinfo, "json", "post", {
				token: localStorage.getItem("token")
			}, function(res) {
				if (res.status == "success") {
					console.log(res.data[0].service_phone)
					localStorage.setItem('appPhone', res.data[0].service_phone);
					return false;
				}
				console.log('未获取到app信息')
			}, function() {
				console.log('未获取到app信息')
			});

			//商户信息 
			require(weburl + modelSYY, "json", "post", {
				token: localStorage.getItem("token")
			}, function(res) {
				//修改商户名称
				if (res.status == "success") {
					console.log(JSON.stringify(res));
					localStorage.setItem('userPhone', res.data.phone);
					localStorage.setItem('storeName', res.data.name);
					$('#app-title').html(localStorage.getItem("storeName"));
					return false;
				}
				console.log('网络错误，未获取到商户信息')
			}, function() {
				console.log('网络错误，未获取到商户信息')
			});

			//活动图片
			require(weburl + activeList, "json", "post", {
				token: localStorage.getItem("token"),
				page: 1,
				number: 1,
				flag: "h",
				category_id: 1
			}, function(e) {
				if ((e.status == "success") && (e.data.length > 0)) {
					$(".active_pt").append("<img src='" + weburls + e.data[0].image + "'/>");
					$(".active_pt").attr("data-id", e.data[0].id);
				}
			}, function(e) {
				//				mui.toast('获取活动列表失败', {
				//					duration: 'long',
				//					type: 'div'
				//				})
			})

			//点击菜单
			mui("body").on("tap", ".mui-table-view-cell", function() {
				var url = $(this).attr("data-url");
				var id = $(this).attr("data-id");
				var url_sign = $(this).attr("data-sign");
				mui.openWindow({
					url: url,
					id: id,
					extras: {
						url_sign: url_sign //扩展参数
					},
					waiting: {
						autoShow: true, //自动显示等待框，默认为true
						aniShow: "slide-in-right", //页面显示动画，默认为”slide-in-right“；
						duration: 200 //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
					}
				})
			})

			//点击最新消息跳转到消息列表页
			mui("body").on("tap", "#news", function() {
				mui.openWindow("newsList.html", "newsList.html");
			})

			//点击收款进行签约判断并跳转
			var time = 0;
			mui("body").on("tap", "#div1_you", function() {
				mui.openWindow("cash.html", "cash.html");
				time = 3;
			})

			mui("body").on("tap", ".modal-item", function() {
				console.log($(this).attr("data-url"));
				mui.openWindow($(this).attr("data-url"), $(this).attr("data-url"));
			})

			//点击扫一扫
			mui("body").on("tap", "#div1_zuo", function() {
				mui.openWindow({
					url: "public/scanning.html",
					id: "scanning.html",
					waiting: {
						autoShow: true, //自动显示等待框，默认为true
						aniShow: "slide-in-right", //页面显示动画，默认为”slide-in-right“；
						duration: 200 //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
					}
				})
			})

			//点击活动
			mui("body").on("tap", ".active_pt", function() {
				mui.openWindow({
					url: "active_Detail.html",
					id: "active_Detail",
					extras: {
						active_id: $(this).attr("data-id") //扩展参数
					},
					waiting: {
						autoShow: true, //自动显示等待框，默认为true
						aniShow: "slide-in-right", //页面显示动画，默认为”slide-in-right“；
						duration: 200 //页面动画持续时间，Android平台默认100毫秒，iOS平台默认200毫秒；
					}
				})
			});
		</script>
	</body>

</html>
